package com.jn.algorithm.day021.question62;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;

/**
 * @author 江南大学1033190417
 * @date 2022/5/13 2:01
 */
public class Solution {

    private static int count = 0;

    //递归
    public int uniquePaths(int m, int n) {
        recursion(0, 0, m, n);
        return count;
    }

    /**
     * @param x 行
     * @param y 列
     * @param m
     * @param n
     */
    public void recursion(int x, int y, int m, int n) {
        if (x == m && y == n) {
            ++count;
            return;
        }
        if (x < m) {
            recursion(x + 1, y, m, n);
            --x;
        }
        if (y < n) {
            recursion(x + 1, y, m, n);
            --y;
        }
    }


    //动态规划
    public int uniquePaths2(int m, int n) {
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; ++i) {
            dp[i][0] = 1;
        }
        for (int i = 0; i < n; i++) {
            dp[0][i] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.uniquePaths2(3, 7));
    }

}
